package com.heima.media.task;

import com.heima.media.entity.WmNews;
import com.heima.media.service.IAuditService;
import com.heima.media.service.IWmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class ArticleDelayTask {

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private IWmNewsService wmNewsService;
    @Autowired
    private IAuditService auditService;
    @Async
    public void receiveMsg() throws Exception{
        RBlockingDeque<Object> blockingDeque = redissonClient.getBlockingDeque("article-queue");
        while (true){
            Object poll = blockingDeque.poll(30, TimeUnit.SECONDS);
            if(poll!=null){
//                poll wmnewId
             Integer wmNewsId = Integer.parseInt( poll.toString()) ;
                WmNews wmNews = wmNewsService.getById(wmNewsId);
                wmNews.setStatus(9);
                Long articleId = auditService.saveApArticle(wmNews);
                wmNews.setArticleId(articleId);
                wmNewsService.updateById(wmNews);
                log.info("已经从延迟队列中获取消息了："+poll);
            }
        }
    }
}
